package com.mogician.len.util;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.annotation.ColorInt;

public class ImageAlterTool
{
    public static ImageAlterTool instance = new ImageAlterTool();

    public Bitmap affine(Bitmap bitmap)
    {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int x1,x2,x3,y1,y2;
        x1 = (int) (width*0.25);
        x2 = (int) (width-x1);
        x3 = (int) (width -width*0.08);
        y1 = (int) (height*0.25);
        y2 = (int) (height - (height*0.08));
        return affine(bitmap, x1, x2, x3, y1, y2);
    }

    public Bitmap affine(Bitmap bitmap, int x1, int x2, int x3, int y1, int y2)
    {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] oldArr, newArr;
        oldArr = new int[width * height];
        newArr = new int[width * height];
        int oldA, oldR, oldG, oldB, newA, newR, newG, newB, index;
        Bitmap newImg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.getPixels(oldArr, 0, width, 0, 0, width, height);
        for (int y=0; y<height; y++)
        {
            for (int x=0; x<width; x++)
            {
                int txa = x1-(x3-x2)*y/height;
                int txb = x2+(x1-txa);
                int tx = txa+(txb-txa)*x/width;
                int ty = y1+(y2-y1)*y/height;
                int i = (y*width+x);
                int j = (ty*width+tx);
                index = oldArr[j];
                oldA = Color.alpha(index);
                oldR = Color.red(index);
                oldG = Color.green(index);
                oldB = Color.blue(index);
                newArr[i] = Color.argb(oldA, oldR,oldG , oldB);
            }
        }
        newImg.setPixels(newArr, 0, width, 0, 0, width, height);
        return newImg;
    }

    public Bitmap colorChange(Bitmap bitmap, ChangeFuction changeFuction)
    {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap newImg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int[] oldArr, newArr;
        oldArr = new int[width * height];
        newArr = new int[width * height];
        bitmap.getPixels(oldArr, 0, width, 0, 0, width, height);
        for(int i = 0; i < width * height; i++)
            newArr[i] = changeFuction.changeColor(oldArr[i]);
        newImg.setPixels(newArr, 0, width, 0, 0, width, height);
        return newImg;
    }

    public Bitmap reverse(Bitmap bitmap)
    {
        return colorChange(bitmap, this::reverse);
    }

    //颜色反转
    private int reverse(@ColorInt int pixel) {
        //拼凑出新的颜色
        return Color.argb(Color.alpha(pixel), 255-Color.red(pixel),
                255-Color.green(pixel), 255-Color.green(pixel));
    }

    public Bitmap grey(Bitmap bitmap)
    {
        return colorChange(bitmap, this::grey);
    }

    /**
     * 配凑灰颜色
     */
    private int grey(@ColorInt int pixel) {
        //拼凑出新的颜色
        int grey = (int) (Color.red(pixel) * 0.3 + Color.green(pixel) * 0.59 + Color.green(pixel) * 0.11);
        return Color.argb(Color.alpha(pixel), grey, grey, grey);
    }
}

@FunctionalInterface
interface ChangeFuction
{
    public int changeColor(@ColorInt int pixel);
}
